<?php
class Product_Controller extends Template_Controller {
	
	var $mr = array();
	var $mlist = array();
	var $search;
	public $template = 'client/index';
	
	public function __construct()
	{
		parent::__construct();		
        
		//Css
        $this->template->css = $this->site['theme_url'].'product/product.css';
		//View		
		$this->get_view();
		$this->search = array('keyword' => '','sort' => '','catid' => '','arr_catid' => '','show' => '','mode' => '','per_page' => '','page' => 0);
	}
	
	public function index()
	{
		$this->template->content = new View('product/home');
		$this->list_new();
		$this->list_hot();
        $this->list_clearance();
	}
	
	public function list_new()
	{
		$this->search['show'] = 1;	
		$this->showlist();
		$this->template->content->list_new = $this->mlist;
	}
	
	public function list_hot()
	{
		$this->search['show'] = 2;		
		$this->showlist();
		$this->template->content->list_hot = $this->mlist;
	}
	
	public function list_clearance()
	{
		$this->search['show'] = 3;		
		$this->showlist();
		$this->template->content->list_clearance = $this->mlist;
	}
	
    private function _get_category()
    {
        //Category
        if(isset($this->search['catid']) && $this->search['catid']
            && is_array($this->search['catid'])){
            $catid = $this->search['catid'];
            $model = new Category_Model();
            $this->db->where('categories.categories_id',$catid[0]);
            $cat = $model->get();
            $this->mr = array_merge($cat[0],$this->mr);
        }
    }
    
	public function cat($id='')
	{
		$this->template->content = new View('product/list');	
		//Get
		if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}
		//Category		
		if($id){
            //Search
            $array = ORM::factory('category',$id)->__get('descendants');
            $arr_cat = array();
            $arr_cat[0] = $id;
            $i=1;
			foreach($array as $node){
				$arr_cat[$i] = $node->categories_id;
                $i++;
			}            
			$this->search['catid'] = $arr_cat;//$id;
			$this->search['keyword'] = '';
			$this->search['arr_catid'] = '';         
            //Get
            $this->_get_category();
		}
		//Set
		$this->session->set_flash('sess_search',$this->search);		
		$this->showlist();
	}
	
	public function mode($name='')
	{
		$this->template->content = new View('product/list');
		//Get
		if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}		
		//Mode
		if(isset($name)){
			$this->search['mode'] = $name;
		}
		//Set
		$this->session->set_flash('sess_search',$this->search);	
		$this->showlist();		
	}
	
	public function sort()
	{
		$this->template->content = new View('product/list');
		//Get
		if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}		
		//Sort
		$sort = $this->input->post('sel_sort');		
		if(isset($sort)){    		
			$this->search['sort'] = $sort;
		}
		//Set
		$this->session->set_flash('sess_search',$this->search);	
		$this->showlist();
	}
	
	public function per_page()
	{
		$this->template->content = new View('product/list');
		//Get
		if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}		
		//Sort
		$per_page = $this->input->post('sel_per_page');		
		if(isset($per_page)){    		
			$this->search['per_page'] = $per_page;
		}
		//Set
		$this->session->set_flash('sess_search',$this->search);	
		$this->showlist();
	}
	
	public function where_sql()
    {
    	if(isset($this->search['catid']) && $this->search['catid'])
            if(is_array($this->search['catid']))
                $this->db->in('products.master_categories_id',$this->search['catid']);
            else
                $this->db->where('products.master_categories_id',$this->search['catid']);
    	if(isset($this->search['keyword']) && $this->search['keyword'])
    	   $this->db->like('products_description.products_name',$this->search['keyword']);
    	if(isset($this->search['arr_catid']) && $this->search['arr_catid'])
			$this->db->in('products.master_categories_id',$this->search['arr_catid']);
		if(isset($this->search['show']) && $this->search['show'])
			$this->db->where('products.products_show',$this->search['show']);
		if(isset($this->search['sort']) && $this->search['sort']){
			if($this->search['sort'] == 'asc')
				$this->db->orderby('products_description.products_name','ASC');
			if($this->search['sort'] == 'desc')
				$this->db->orderby('products_description.products_name','DESC');
			if($this->search['sort'] == 'asc_price'){
				$this->db->orderby('products.products_our_price','ASC');
				//$this->db->orderby('products.products_sale_price','ASC');
			}
			if($this->search['sort'] == 'desc_price'){
				$this->db->orderby('products.products_our_price','DESC');
				//$this->db->orderby('products.products_sale_price','DESC');
			}
			if($this->search['sort'] == 'looked')
				$this->db->orderby('products.products_looked','DESC');
			if($this->search['sort'] == 'purchased')
				$this->db->orderby('products.products_purchased','DESC');
		}			
		//print_r($this->search);		
    }
	
	public function search()
	{
		$this->template->content = new View('product/list');	
		//Get
		if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}
		//Keyword
		$keyword = $this->input->post('txt_keyword');
		if(isset($keyword)){    		
			$this->search['keyword'] = $keyword;
			$this->search['catid'] = '';
		}		
		//Category search
		$cate_search = $this->input->post('cb_search');
		if(isset($cate_search)){
		 	$array= ORM::factory('category',$cate_search)->__get('descendants')->as_array();
            $arr_cat = '';
			foreach($array as $key=>$value){
				$arr_cat[$key] = $value->categories_id;
			}
			$this->search['arr_catid'] = $arr_cat;
			$this->search['catid'] = '';
		}
		//Set
		$this->session->set_flash('sess_search',$this->search);
		
		$this->showlist();
	}
	
	public function showlist()
	{	
		//Assign		
		if((isset($this->search['keyword']) && $this->search['keyword']) 
		|| (isset($this->search['arr_catid']) && $this->search['arr_catid']))
			$this->template->content->search = 1;
		if(isset($this->search['mode']) && $this->search['mode'])
			$this->template->content->mode = $this->search['mode'];
		if(isset($this->search['sort']) && $this->search['sort'])
			$this->template->content->sort = $this->search['sort'];
		if(isset($this->search['per_page']) && $this->search['per_page']){
			$this->template->content->per_page = $this->search['per_page'];
			$per_page = $this->search['per_page']; 
		}

		$mlist;		
		$model = new Product_Model();
		$this->where_sql();		
		 
		if($this->login_model->get_login(1)){
			// check type customer
	  		$cus = $this->login_model->get_login(1);	
	   		$type = $cus['customers_type'];
				
			if($type==1){
				$mlist = $model->getdata('',1,$this->get_client_lang());
			} else {
				$mlist = $model->getdata('','',$this->get_client_lang());
			}
		} else {
			$mlist = $model->getdata('','',$this->get_client_lang());
		}
		$this->template->content->total = count($mlist);
		//Per page
		if(!isset($per_page))
			$per_page = $this->site['site_num_line']; 
		$this->pagination = new Pagination(array(
			'base_url'    => 'product/search/',
			'uri_segment'    => 'page',
			'total_items'    => count($mlist),
			'items_per_page' => $per_page,
			'style'          => 'digg',
		));	
		$this->db->limit($this->pagination->items_per_page,$this->pagination->sql_offset);		
		
		$this->where_sql();
		if($this->login_model->get_login(1)){
			// check type customer
		  	$cus = $this->login_model->get_login(1);	
		   	$type = $cus['customers_type'];
				
			if($type==1){
				$mlist = $model->getdata('',1,$this->get_client_lang());
			} else {
				$mlist = $model->getdata('','',$this->get_client_lang());
			}
		} else {
			$mlist = $model->getdata('','',$this->get_client_lang());
		}		
		$this->mlist = $mlist; //echo $this->db->last_query();
		$this->template->content->mlist = $mlist;
        //Category
        $this->_get_category();
        $this->template->content->mr = $this->mr;
	}
	
	private function _update_looked($id)
	{
		$result = ORM::factory('product')->find($id)->as_array();
		$result['products_looked'] += 1;
		$this->db->update('products',$result,array('products_id' => $id));
	}
	
	public function detail($id='',$cate='')
	{
		$this->template->content = new View('product/detail');
		$mlist;
		$model = new Product_Model();
		if($this->login_model->get_login(1)){
			// check type customer
		  	$cus = $this->login_model->get_login(1);	
		   	$type = $cus['customers_type'];
				
			if($type==1){
				$mlist = $model->getdata($id,1,$this->get_client_lang());
			} else {
				$mlist = $model->getdata($id,'',$this->get_client_lang());
			}
		} else {
			$mlist = $model->getdata($id,'',$this->get_client_lang());
		}
		if($id)
			$this->_update_looked($id);
		$this->template->content->mr = $mlist[0];
	}
}
?>